package cascade.controller.pages.commands
{
	import cascade.model.Page;
	import cascade.model.Revision;
	
	import flight.commands.AsyncCommand;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.Fault;

	public class CommandLoadRevisions extends AsyncCommand
	{
		protected var page:Page;
		
		public function CommandLoadRevisions(page:Page)
		{
			this.page = page;
		}
		
		public override function execute():*
		{
			Cascade.busy.addBusy(this);
			
			Cascade.service.SiteService.getRevisions(page.id, result, fault);
		}
		
		protected function result(revisions:Array):void {
			page.revisions = new ArrayCollection(revisions);
			
			page.draftRevision = revisions[revisions.length - 1];
			if (revisions.length > 1)
				page.liveRevision = revisions[revisions.length - 2];
			
			Cascade.busy.removeBusy(this);
			
			dispatchExecutionComplete();
		}
		
		protected function fault(info:Object):void {
			trace(info);
			
			Cascade.busy.removeBusy(this);
		}
	}
}